
import { provide, inject, ref, Ref } from 'vue';

/** 修改状态的类型 */
interface UpdateState {
  (): void
}

/** 返回状态的类型 */
interface ProvideInject {
  /** 初始化状态 */
  isCollapse: Ref<boolean>,
  /** 修改状态方法 */
  updateisCollapse: UpdateState
}


/** 注入状态的 key值 Symbol类型  */
export const stateSymbol = Symbol('isCollapse');
/** 初始化创建状态的方法
 * @returns {ProvideInject}
 */
export const initState = (): ProvideInject => {
  const isCollapse: Ref<boolean> = ref(false);
  const updateisCollapse: UpdateState = () => {
    isCollapse.value = !isCollapse.value;
  };
  return { updateisCollapse, isCollapse }
};

/** 注入-状态
 * @returns {ProvideInject}
 */
export const injectState = (): ProvideInject => (inject(stateSymbol) as ProvideInject);

/** 供应-状态
 * @returns {ProvideInject}
 */
export const provideState = (): ProvideInject => {
  const a = initState()
  provide(stateSymbol, a)
  return a
}